﻿using System;
using Common;
using CommonDao;
using QQNumberForZoneCatchDao;
using Spider.Service;

namespace QQNumberForZoneCatchService
{
    public class QQNumberForZoneService
    {
        private static readonly object SyncBoot = new object();
        public static string CurrentQqSeed { get; set; }
        public static string StartToCatchEmailAddress(string seedUrl)
        {
            SpiderService.AddUrlToTodoQueue(new string[]
            {
                seedUrl
            });
            SpiderService.CommandSpiderToWork();
            AntService.CommandAntCaptainToWork();
            return "";
        }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
        /// <summary>
        /// 开始QQ邮箱种子生成到队列
        /// </summary>
        /// <param name="seedcount"></param>
        public static void StartToQQEmailGenerate()
        {
            SpiderService.CommandSpiderToWork();
            AntService.CommandAntCaptainToWork();
        }
        /// <summary>
        /// 自动定时填充QQ种子到队列
        /// </summary>
        /// <param name="seedcount"></param>
        public static void AutoSendQQSeed(int seedcount)
        {
            var taskcount = SpiderMonitor.GetTodoTaskCount();
            if (taskcount == 0)
            {
                UInt64 seed2;
                lock (SyncBoot)
                {
                    var seed = SystemConfigDao.Get(SystemConfigKey.QQEmailSeed);
                    if (string.IsNullOrWhiteSpace(seed))
                    {
                        seed2 = 10201;
                    }
                    else
                    {
                        seed2 = Convert.ToUInt64(seed);
                    }
                    for (int i = 0; i < seedcount; i++)
                    {
                        seed2++;
                        SpiderService.AddContentToTodoQueue(new[] { seed2.ToString() },"qqSeed");
                    }
                    SystemConfigDao.Update(SystemConfigKey.QQEmailSeed, seed2.ToString());
                    CurrentQqSeed = seed2.ToString();
                }
                
            }
        }
    }
}
